package com.century.sjt.activity.Topic;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.century.sjt.R;
import com.century.sjt.activity.BaseActivity;
import com.century.sjt.util.Constant;
import com.century.sjt.widget.TitleBar.SjtTitleBar;
import com.century.sjt.widget.picture.IBaseConstant;
import com.century.sjt.widget.picture.PictureBean;
import com.century.sjt.widget.picture.PictureItemFragment;

import java.util.ArrayList;
import java.util.List;

public class ZonePictureActivity extends BaseActivity implements ViewPager.OnPageChangeListener, IBaseConstant {

    private static final String STATE_POSITION = "STATE_POSITION";

    /**
     * 当前默认选中位置
     */
    private int mDefaultIndex = 0;

    /**
     * 图片数据源
     */
    private ArrayList<PictureBean> dataSource = new ArrayList<PictureBean>();

    /**
     * 可滑动的图片查看器
     */
    private ViewPager mImageViewPager;

    /**
     * 图片VP适配器
     */
    private ImagePagerAdapter mImageVpAdapter;

    /**
     * 指示器
     */
    private TextView indicator;

//    private HorizontalListView hlvContent;

//    private HorizontalListViewAdapter adapter;

    private RequestQueue mQueue;

    private SjtTitleBar titleBar;

    private RelativeLayout rlPhotelist;

    private String username="";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zone_picture);

        initBundle(savedInstanceState);

        initView();

        initEvent();


    }

    private void initBundle(Bundle savedInstanceState) {

        //设置上一页传递的position
//        if (savedInstanceState != null) {
//            mDefaultIndex = savedInstanceState.getInt(STATE_POSITION);
//        }

        Bundle bundle = getIntent().getExtras();
        try {
            //设置上一页传递的position
            mDefaultIndex = bundle.getInt(PICTURE_VIEWER_DEFAULT_POSTION, mDefaultIndex);
            String[] imageUrls = (String[]) bundle.getSerializable(PICTURE_VIEWER_DATASOURCE);
            if (null != imageUrls && imageUrls.length > 0) {
                for (int i = 0; i < imageUrls.length; i++) {
                    PictureBean pictureBean = new PictureBean();
                    pictureBean.setFilePath(imageUrls[i]);
                    pictureBean.setFileName("unChecked");
                    dataSource.add(pictureBean);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(Constant.TAG, "获取参数发生异常，原因：" + e.getMessage());
        }

    }
    @Override
    protected void initView() {
        titleBar = (SjtTitleBar) findViewById(R.id.title_bar);
        indicator = (TextView) findViewById(R.id.indicator);
//        hlvContent = (HorizontalListView) findViewById(R.id.hlv_content);
        mImageViewPager = (ViewPager) findViewById(R.id.pager);
        rlPhotelist = (RelativeLayout) findViewById(R.id.rl_photelist);
    }
    @Override
    protected void initEvent() {
        //标题栏
        titleBar.setTitle(getResources().getString(R.string.tab_picture_see));
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //图片Viewpager、
        mImageVpAdapter = new ImagePagerAdapter(getSupportFragmentManager(), dataSource);
        mImageViewPager.setAdapter(mImageVpAdapter);

        //指示文本
//        CharSequence text = getString(R.string.viewpager_indicator, 0 + 1, mImageVpAdapter.getCount());
//        indicator.setText(text);
//        mImageViewPager.setCurrentItem(0);
        //设置上一页传递的position
        CharSequence text = getString(R.string.viewpager_indicator, mDefaultIndex + 1, mImageVpAdapter.getCount());
        indicator.setText(text);
        mImageViewPager.setCurrentItem(mDefaultIndex);

        //横向listview
//        adapter = new HorizontalListViewAdapter(ZonePictureActivity.this, dataSource, mQueue);
//        hlvContent.setAdapter(adapter);
//        hlvContent.smoothScrollToPosition();

//        hlvContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                mImageViewPager.setCurrentItem(position);//点击小图，大图移动到对应的界面
//            }
//        });

        mImageViewPager.setOnPageChangeListener(this);

//设置上一页传递的position
        notifyDataSetChangedForAdapter(mDefaultIndex);//是否选中的标志更新
//        hlvContent.setSelection(mDefaultIndex);//小图移动到对应位置
//        notifyDataSetChangedForAdapter(0);//是否选中的标志更新
//        hlvContent.setSelection(0);//小图移动到对应位置
    }

    @Override
    protected void initData() {

    }


    public void notifyDataSetChangedForAdapter(int position) {
        for (int i = 0; i < dataSource.size(); i++) {
            dataSource.get(i).setFileName("unChecked");
        }
        dataSource.get(position).setFileName("checked");
//        adapter.notifyDataSetChanged();

    }

//    @Override
//    public void onWindowFocusChanged(boolean hasFocus) {
//        super.onWindowFocusChanged(hasFocus);
//        if(hasFocus){
//            //设置上一页传递的position
//            notifyDataSetChangedForAdapter(mDefaultIndex);//是否选中的标志更新
//            hlvContent.setSelection(mDefaultIndex);//小图移动到对应位置
////            notifyDataSetChangedForAdapter(0);//是否选中的标志更新
////            hlvContent.setSelection(0);//小图移动到对应位置
//        }
//    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_POSITION, mImageViewPager.getCurrentItem());
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        CharSequence text = getString(R.string.viewpager_indicator, position + 1, mImageViewPager.getAdapter().getCount());
        indicator.setText(text);
        notifyDataSetChangedForAdapter(position);//是否选中的标志更新
//        hlvContent.setSelection(position);//移动大图，小图移动到对应位置

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 图片适配器
     */
    class ImagePagerAdapter extends FragmentStatePagerAdapter {

        public List<PictureBean> fileList;

        public ImagePagerAdapter(FragmentManager fm, List<PictureBean> fileList) {
            super(fm);
            this.fileList = fileList;
        }

        @Override
        public int getCount() {
            return fileList == null ? 0 : fileList.size();
        }

        @Override
        public Fragment getItem(int position) {
//            PictureItemFragment pictureItemFragment = new PictureItemFragment();
//            pictureItemFragment.setUserName(username);
            return PictureItemFragment.newInstance(fileList.get(position), username, Constant.SHOP_COUNT_LIST);
        }
    }

}


